<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="命令集名称" prop="cmdName">
        <el-input
          v-model="queryParams.cmdName"
          placeholder="请输入命令集名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="命令集说明" prop="cmdRemark">
        <el-input
          v-model="queryParams.cmdRemark"
          placeholder="请输入命令集说明"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['kong:cmdset:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['kong:cmdset:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['kong:cmdset:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['kong:cmdset:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cmdsetList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="gid" />
      <el-table-column label="命令集名称" align="center" prop="cmdName" />
      <el-table-column label="命令集说明" align="center" prop="cmdRemark" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-fold"
            @click="stepManger(scope.row)"
            v-hasPermi="['kong:cmdset:manager']"
          >步骤管理</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['kong:cmdset:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['kong:cmdset:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改命令集对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="命令集名称" prop="cmdName">
          <el-input v-model="form.cmdName" placeholder="请输入命令集名称" />
        </el-form-item>
        <el-form-item label="命令集说明" prop="cmdRemark">
          <el-input v-model="form.cmdRemark" placeholder="请输入命令集说明" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!--  步骤管理抽屉  -->
    <el-drawer
      title="步骤管理"
      size="50%"
      :visible.sync="drawer"
      :with-header="false">
      <div style="padding: 20px">
        <el-timeline>
          <el-timeline-item v-for="(step, index) in steps" :timestamp="step.controlName" placement="top" >
            <el-card>
              <el-form :ref="step"  label-width="150px" size="small">
                <el-form-item label="步骤名称"  >
                  <el-input v-model="step.controlName"  placeholder="请输入步骤名称" size="80" />
                </el-form-item>
                <el-form-item label="步数" >
                  <el-input v-model="step.step"  placeholder="请输入执行此类的第几步" size="80" type="number" />
                </el-form-item>
                <el-form-item label="延迟时间(S)" >
                  <el-input v-model="step.delay"  placeholder="请输入延迟多少S执行" size="80" type="number" />
                </el-form-item>
                <el-form-item label="选择控件类型" >
                  <el-select v-model="step.executeClassName">
                    <el-option
                      v-for="item in dict.type.setp_execute_class_name"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="操作类型" v-show="step.executeClassName!='com.zeroup.auto.assists.step.unify.StartAppStep'">
                  <el-select v-model="step.stepServiceDataObj.acType" >
                    <el-option
                      v-for="item in acType"
                      :key="item.val"
                      :label="item.an"
                      :value="item.val">
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="控件ID还是文本" v-show="step.executeClassName!='com.zeroup.auto.assists.step.unify.StartAppStep'">
                  <el-select v-model="step.stepServiceDataObj.seachType" >
                    <el-option
                      v-for="item in seachType"
                      :key="item.val"
                      :label="item.an"
                      :value="item.val">
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="选择第几个控件" v-show="step.executeClassName!='com.zeroup.auto.assists.step.unify.StartAppStep'">
                  <el-input v-model="step.stepServiceDataObj.selectControlIndex"  placeholder="选择第几个控件" size="80" />
                </el-form-item>
                <el-form-item label="控件ID"  v-show="step.stepServiceDataObj.seachType==1">
                  <el-input v-model="step.stepServiceDataObj.controlId"  placeholder="请输入控件ID" />
                </el-form-item>
                <el-form-item label="控件文本"  v-show="step.stepServiceDataObj.seachType==2">
                  <el-input v-model="step.stepServiceDataObj.controlTxt"  placeholder="请输入控件文本" />
                </el-form-item>
                <el-form-item label="控件类名"  v-show="step.stepServiceDataObj.seachType==3">
                  <el-input v-model="step.stepServiceDataObj.controlTxt"  placeholder="请输入控件类名" />
                </el-form-item>
                <el-form-item label="是否需要填充数值"  v-show="step.executeClassName!='com.zeroup.auto.assists.step.unify.StartAppStep'">
                  <el-radio v-model="step.stepServiceDataObj.isFillVal" label="y">是</el-radio>
                  <el-radio v-model="step.stepServiceDataObj.isFillVal" label="n">否</el-radio>
                </el-form-item>
                <el-form-item :label="step.executeClassName=='com.zeroup.auto.assists.step.unify.StartAppStep'?'启动包名称':'控件操作数值'" prop="cmdRemark"  >
                  <el-input v-model="step.stepServiceDataObj.dataValue"  placeholder="请输入文本控件输入的值" />
                </el-form-item>
                <el-form-item label="" prop="cmdRemark">
                    <el-button type="danger" @click="delStep(step,index)" icon="el-icon-minus">删除此步骤</el-button>
                </el-form-item>
              </el-form>
            </el-card>
            <el-button v-show="steps.length-1==index" type="primary" @click="addStep(index)" icon="el-icon-plus">新 增 步 骤</el-button>
          </el-timeline-item>
        </el-timeline>


        <el-button type="success" @click="saveStep" icon="el-icon-coin" :loading="saveAllloading">保 存</el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { listCmdset, getCmdset, delCmdset, addCmdset, updateCmdset } from "@/api/kong/cmdset";
import {addStep, addStepAll, getCmdSetAllStep} from "@/api/kong/step";

export default {
  name: "Cmdset",
  dicts: ['setp_execute_class_name'],
  data() {
    return {
      saveAllloading:false,
      acType:[{an:"点击",val:1},{an:"输入",val:2}],
      seachType:[{an:"ID",val:1},{an:"文本",val:2},{an:"类名",val:3}],
      drawer: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 命令集表格数据
      cmdsetList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cmdName: null,
        cmdRemark: null,
      },
      form:{

      },
      rules:{

      },
      steps:[{
        gid: null,
        cmdSetGid: null,
        controlName: '步骤1',
        executeClassName:"",
        step:1,
        delay:0,
        parentGid:1,
        stepServiceDataObj:{
          acType:"",
          seachType:"",
          selectControlIndex:1,
          controlId:"",
          controlTxt:"",
          dataValue:"",
          isFillVal:'n'
        }
      }],
      currentCmdSet:{}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询命令集列表 */
    getList() {
      this.loading = true;
      listCmdset(this.queryParams).then(response => {
        this.cmdsetList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        gid: null,
        cmdName: null,
        cmdRemark: null,
        createTime: null,
        createBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.gid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加命令集";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const gid = row.gid || this.ids
      getCmdset(gid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改命令集";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.gid != null) {
            updateCmdset(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCmdset(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const gids = row.gid || this.ids;
      this.$modal.confirm('是否确认删除命令集编号为"' + gids + '"的数据项？').then(function() {
        return delCmdset(gids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('kong/cmdset/export', {
        ...this.queryParams
      }, `cmdset_${new Date().getTime()}.xlsx`)
    },
    stepManger(row){//命令集步骤管理
      this.drawer = true
      this.currentCmdSet = row
      const that = this;
      getCmdSetAllStep(this.currentCmdSet.gid).then(response =>{
        console.log(response)
        if(response.length>0){
          this.steps.splice(0,this.steps.length)
          that.steps = response
        }else{
          that.steps[0].cmdSetGid = this.currentCmdSet.gid
        }
      })
    },
    addStep(currentIndex){
      let index = this.steps.length + 1
      this.steps.push({
        gid:null,
        cmdSetGid: this.currentCmdSet.gid,
        controlName: '步骤' + index,
        executeClassName:"",
        step:1,
        delay:0,
        parentGid: index,
        stepServiceDataObj:{
          acType:"",
          seachType:"",
          selectControlIndex:1,
          controlId:"",
          controlTxt:"",
          dataValue:""
        }
      });
    },
    saveStep(){
      this.saveAllloading = true;
      addStepAll(this.steps).then(response =>{
        if(response.code===200){
          this.$modal.msgSuccess("保存成功");
        }
      }).finally(i=>{
        this.saveAllloading = false;
      })
    },
    delStep(step,index){
      this.steps.splice(index,1)
      this.steps.forEach((item,i)=>{
        this.$set(this.steps[i],"parentGid", i + 1)
      })
    }
  }
};
</script>
